package com.manage.controller;

import com.beans.FollowRecord;
import com.beans.page.PageList;
import com.commons.util.StringUtil;
import com.manage.util.UserUtils;
import com.services.FollowRecordService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @author jiangwei
 * @since 18/1/19
 */
@Controller
@RequestMapping("follow")
public class FollowController extends BaseController {
    @Autowired
    public FollowRecordService followRecordService;

    /**
     * 查询列表
     *
     * @param followRecord
     * @return
     */
    @RequiresPermissions("follow:view")
    @RequestMapping(value = {"", "/", "list"})
    public String list(FollowRecord followRecord, Long relationType, Long relationId, HttpServletRequest request) {

        followRecord.setRelationId(relationId);
        followRecord.setRelationType(relationType);
        request.setAttribute("follow", followRecord);
        return "follow/list";
    }

    /**
     * 异步获取list资源
     *
     * @param pageList
     * @param followRecord
     * @return
     */
    @RequiresPermissions("follow:view")
    @RequestMapping("asynList")
    @ResponseBody
    public PageList asynList(PageList pageList, FollowRecord followRecord) {
        return followRecordService.queryPage(pageList, followRecord);
    }

    @RequiresPermissions("follow:edit")
    @RequestMapping("save")
    @ResponseBody
    public boolean add(Long relationId, String followInfo, String followDate, Long relationType){
        boolean returnValue = false;

        FollowRecord followRecord = new FollowRecord();

        followRecord.setCreateBy(UserUtils.getUser().getUserId());
        followRecord.setUpdateBy(UserUtils.getUser().getUserId());
        followRecord.setRelationType(relationType);

        followRecord.setUpdateDate(StringUtil.StirngToTimestamp(followDate + " 00:00:00"));
        followRecord.setRelationId(relationId);
        followRecord.setText(followInfo);

        int rsout = followRecordService.add(followRecord);

        if(rsout > 0){
            returnValue = true;
        }

        return returnValue;
    }
}
